可点击行不会影响按钮

时间:2017-04-18 20:05:08

标签: javascript jquery python html django

.html文件是

    <tbody>
        {% for row in results %}
        <tr class="{% cycle 'row1' 'row2' %} clickable-row" data-href="{% url "perception:detail"  pk=row.object.pk %}">
            {% for item in row.cols %}
            {{ item }}
            {% endfor %}
            {% if row_actions_template %}
            <td class="row-actions">{% include row_actions_template with object=row.object %}</td>
            {% endif %}
        </tr>
        {% endfor %}
    </tbody>

.js文件是

$(function(e){
    $(".clickable-row").click(function() {
        window.location = $(this).data("href");
    });
});

views.py文件是

class PerceptionIndexView(StaffRestrictedMixin, FrontendListView):
    page_title = _('Perception')
    model = Perception
    template_name = 'loanwolf/perception/index.html'
    pjax_template_name = 'loanwolf/perception/index.pjax.html'
    # row_actions_template_name = 'loanwolf/perception/list-actions.inc.html'
    url_namespace = 'perception'

def active(self, obj):
    if obj.is_active:
        return icon(obj.get_icon(), css_class='green-text', tooltip=_('Active'))
    else:
        return icon(obj.get_icon(), css_class='red-text', tooltip=_('Inactive'))

def notes_count(self, obj):
    return obj.notes.count()
notes_count_label = _('Notes')

def get_change_url(self, obj):
    return obj.get_absolute_url()

class Meta:
    ordering = ('-created', '-modified')
    sortable = ('start_date', 'end_date', 'created', 'state', 'modified')
    list_filter = ('state', 'is_active', customer, error)
    list_display = (
        '__unicode__', 'context_menu', 'state', 'start_date', 'end_date', 'current_balance',
        'operation_error', 'modified', 'created', 'notes_count', 'active'
    )

models.py文件的一部分是

@python_2_unicode_compatible
class Perception(xwf_models.WorkflowEnabled, TimeStampedModel):
    loan = models.ForeignKey('loans.Loan')
    state = xwf_models.StateField(PerceptionWorkflow)
    start_date = models.DateField(_('Start date'))
    end_date = models.DateField(_('End date'), blank=True, null=True)

    current_balance = models.DecimalField(_('Current balance'),
                        default=0, decimal_places=2, max_digits=11)
    operation_error = models.SmallIntegerField(_('Operation error'), default=0)
    notes = GenericRelation(Note)



def get_absolute_url(self):
    return reverse('perception:detail', kwargs={'pk': self.pk})

def get_icon(self):
    if self.is_active:
        return 'check_circle'
    else:
        return 'remove_circle'

def save(self, *args, **kwargs):
    rs = super(Perception, self).save(*args, **kwargs)
    return rs

#Property
def __str__(self):
    return six.text_type(_('Perception #%07d') % self.pk)

@property
def firstname(self):
    first_name = self.loan.request.customer.user.first_name
    return first_name

@property
def lastname(self):
    last_name = self.loan.request.customer.user.last_name
    return last_name

@property
def context_menu(self):
    tpl = 'perception/context-menu.inc.html'
    return mark_safe(render_to_string(tpl, {
        'user': self.loan.request.customer.user,
        'customer': self.loan.request.customer,
    }))

perception/context-menu.inc.html文件看起来像

{% load i18n %}
<div class="customer-context-menu closed {% if customer.gender == 0 %}male{% else %}female{% endif %}">
    <b class="unselectable">
        {{ customer.icon }}
        {{ user.get_full_name }}
    </b>
    <ul>
        <li class="tip"></li>
        <li><a href="{% url "customers:profile" cust=customer.pk %}" class="unselectable" data-turbolinks="false">{% trans "Profile" %}</a></li>
        <li><a href="{% url "customers:alerts_index" cust=customer.pk %}" class="unselectable" data-turbolinks="false">{% trans "Alerts" %}</a></li>
        <li><a href="{% url "customers:messaging" cust=customer.pk %}" class="unselectable" data-turbolinks="false">{% trans "Messaging" %}</a></li>
        <li><a href="{% url "customers:requests" cust=customer.pk %}" class="unselectable" data-turbolinks="false">{% trans "Requests" %}</a></li>
        <li><a href="{% url "customers:documents" cust=customer.pk %}" class="unselectable" data-turbolinks="false">{% trans "Documents" %}</a></li>
        <li><a href="{% url "customers:logs" cust=customer.pk %}" class="unselectable" data-turbolinks="false">{% trans "Logs" %}</a></li>
        <li class="separator"></li>
        {% if customer.phone_1 %}
        <li class="phone">{{ customer.phone_1 }}</li>
        {% endif %}
        <li><a href="mailto:{{ user.email }}" data-turbolinks="false"><i class="material-icons">email</i> {{ user.email }}</a></li>
        <li><a href="{% url "customers:print" cust=customer.pk %}" class="unselectable" data-turbolinks="false" target="_blank"><i class="material-icons">printer</i> {% trans "Print" %}</a></li>
    </ul>
</div>

row image中,我可以点击与Randy Senger相关联的按钮,该按钮在同一页面上打开一个与enter image description here类似的不同选项的小窗口。实际上,此行有一个clickable-row。当我点击按钮时,会出现问题。当我点击按钮时,它打开小窗口大约两秒钟,然后在另一页面上渲染一点,好像我点击了这一行。我想我可以在preventDefault文件上使用.js,这样当我点击按钮时,它就不会受clickable-row的影响。任何人都可以知道如何修改我的.js文件来修复此问题吗?

P.S。如果问题不清楚,请告诉我。

这里应该是什么问题?

$(function(e){
    $(".clickable-row").on('click', function() {
        window.location = $(this).data("href");
    });
});

$(".customer-context-menu").on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();
});

1 个答案:

答案 0 :(得分:0)

不幸的是,我对Python一无所知。但似乎你的问题是关于在DOM上包装元素的事件。这与事件冒泡有关。

如果您将事件绑定到父级和子级,则单击该子级将触发这两个事件,除非您指定应该执行事件的上下文。

event.stopPropagation应该阻止事件处理通知父母。

$('.child').on('click', function(){
    e.stopPropagation();
    console.log('only the child fires the event');
});

在任何情况下,您是否尝试检索jQuery事件的event.target?根据您在父母内部点击的位置,它应该是不同的。

<script>
    $('.clickable-row').on('click', function(event){
        // Every event returns an event object when specified as a callback parameter
        console.log(event.target);

        if ($(event.target).is('.my-class')){
            // Do code for specific clicked element
        }
    });
</script>

我真的希望这有帮助!